package listeners;


import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import com.mysql.jdbc.Statement;

import classes.AccountManager;
import classes.DBManager;
import classes.GroupManager;
import classes.HomeworkManagerLecturer;
import classes.HomeworkManagerStudent;
import classes.SubjectManager;

/**
 * Application Lifecycle Listener implementation class contextListener
 *
 */
@WebListener
public class contextListener implements ServletContextListener {
	private static final String SERVER = "localhost";
	private static final String USER_NAME = "root";
	private static final String PASSWORD = "macs";
	private static final String DATABASE = "test";
	
	
    /**
     * Default constructor. 
     */
    public contextListener() {}

	/**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent arg0) {
    	DBManager manager = new DBManager(SERVER, USER_NAME, PASSWORD, DATABASE);
		arg0.getServletContext().setAttribute(DBManager.DBS_MANAGER, manager);
		arg0.getServletContext().setAttribute(AccountManager.ACCOUNTMANAGER,
				new AccountManager(manager.getStatement()));
		arg0.getServletContext().setAttribute(SubjectManager.SUBJECTMANAGER,
				new SubjectManager(manager.getStatement()));
		arg0.getServletContext().setAttribute(GroupManager.GROUPMANAGER,
				new GroupManager((Statement) manager.getStatement()));
		arg0.getServletContext().setAttribute(
				HomeworkManagerLecturer.HOMEWORKMANAGERLECTURER,
				new HomeworkManagerLecturer(manager.getStatement()));
		arg0.getServletContext().setAttribute(
				HomeworkManagerStudent.HOMEWORKMANAGERSTUDENT,
				new HomeworkManagerStudent(manager.getStatement()));  
    }

	/**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent arg0) {}
	
}
